package com.jakewharton.rxbinding4.component;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.openharmony.MainThreadDisposable;
import ohos.agp.components.TickTimer;

final class TickTimerUpdateObservable extends Observable<TickTimer> {
    private final TickTimer view;

    TickTimerUpdateObservable(@NonNull TickTimer view) {
        this.view = view;
    }

    @Override
    protected void subscribeActual(@NonNull Observer<? super TickTimer> observer) {
        Listener listener = new Listener(view, observer);
        observer.onSubscribe(listener);
        view.setTickListener(listener);
    }

    static final class Listener extends MainThreadDisposable implements TickTimer.TickListener {
        private final TickTimer view;
        private final Observer<? super TickTimer> observer;

        Listener(TickTimer view, Observer<? super TickTimer> observer) {
            this.view = view;
            this.observer = observer;
        }

        @Override
        protected void onDispose() {
            view.setTickListener(null);
        }

        @Override
        public void onTickTimerUpdate(TickTimer tickTimer) {
            if (!isDisposed()) {
                observer.onNext(tickTimer);
            }
        }
    }
}
